package com.fengmi.controller;

import com.fengmi.entity.SysUser;
import com.fengmi.service.UserService;
import com.fengmi.vo.ResultVO;
import com.util1.JwtUtils;
import com.util1.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;

/**
 * @Author 123
 * @Date 2022/1/25 10:09
 * @Version 1.0
 */


@RestController
@RequestMapping("u")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("login")
    public ResultVO login(@RequestBody SysUser sysUser){

        return  userService.login(sysUser);

    }


    @RequestMapping("check")
    public ResultVO check(@RequestHeader String token){
        //校验令牌合法
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(ResourceUtils.getFile("classpath:rsa.pub").getPath());
            Object infoFromToken = JwtUtils.getInfoFromToken(token, publicKey, SysUser.class);

            return  new ResultVO(true,"valid token");
        } catch (Exception e) {
            e.printStackTrace();
            return  new ResultVO(false,"invalid token");
        }
    }
}
